# -*- coding: utf-8 -*-
class WarehouseTypeController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:newWarehouseType,:editWarehouseType,:warehouseTypeLock]

  def warehouseTypeList

  end

  def create
    begin
      warehouseType = WarehouseType.new
      warehouseType.company_id =  session[:company_id]
      warehouseType.department_id =  session[:department_id]
      warehouseType.employee_id =  session[:employee_id]
      warehouseType.name = params[:name]
      warehouseType.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end

    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  def update
    begin
      warehouseType = WarehouseType.find(params[:id])
      warehouseType.employee_id =  session[:employee_id]
      warehouseType.name = params[:name]
      warehouseType.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      logger.info info
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end

    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁仓库类型
  def warehouseTypeLock
    warehouseType = WarehouseType.find(params[:id])
    if warehouseType.is_locked == 1
      warehouseType.is_locked = 0
    else
      warehouseType.is_locked = 1
    end
    warehouseType.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  
  def getAllWarehouseTypes
    forCBB = params[:for_cbb]
    if forCBB == 'true'
      warehouseTypes = WarehouseType.where("is_locked = ?",0)
    else
      warehouseTypes = WarehouseType.all
    end
    
    jsonStr="{totalProperty:"+warehouseTypes.length.to_s+",root:"+warehouseTypes.to_json()+"}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def getWarehouseTypeById
    warehouseType = WarehouseType.find(params[:id])
    rootStr = warehouseType.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    #logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end
end
